package mysql

import "gorm.io/gorm"

type User struct { //新建用户结构体模拟一条用户数据
	gorm.Model
	Account    string `json:"account" gorm:"type:varchar(20);not null;index;comment:'账号'"`
	Password   string `json:"password" gorm:"type:char(32);not null;comment:'密码'"`
	UserStatus int    `json:"userStatus" gorm:"type:int(1);not null;comment:'用户状态1-正常，2-非正常'"`
}

type Order struct {
	gorm.Model
	UserId    int     `json:"userId" gorm:"type:int(10);not null;comment:'用户编号'"`
	OrderSn   string  `json:"orderSn" gorm:"type:varchar(36);not null;index;comment:'订单编号'"`
	Payment   string  `json:"payment" gorm:"type:varchar(20);not null;comment:'支付方式'"`
	VipPrice  float64 `json:"vipPrice" gorm:"type:float(10,2);not null;comment:'折扣金额'"`
	Price     float64 `json:"price" gorm:"type:float(10,2);not null;comment:'支付金额'"`
	Num       int     `json:"num" gorm:"int(10);not null;comment:'下单数量'"`
	PayTime   string  `json:"payTime" gorm:"type:varchar(100);not null;comment:'支付时间'"`
	PayStatus int     `json:"payStatus" gorm:"type:int(1);not null;comment:'1-待支付，2-已支付，3-支付失败'"`
}

type OrderGoods struct {
	gorm.Model
	OrderSn string  `json:"orderSn" gorm:"type:varchar(36);not null;index;comment:'订单编号'"`
	Title   string  `json:"title" gorm:"type:varchar(100);not null;comment:'标题'"`
	Cover   string  `json:"cover" gorm:"type:varchar(200);not null;comment:'封面'"`
	Price   float64 `json:"price" gorm:"type:float(10,2);not null;comment:'资源单价'"`
}

func (u *User) Login(account string) error {
	return DB.Model(&User{}).Where("account=?", account).Limit(1).Find(&u).Error
}

func (o *Order) CreateOrder() error {
	return DB.Model(&Order{}).Create(&o).Error
}

func (og *OrderGoods) CreateOrderGoods() error {
	return DB.Model(&OrderGoods{}).Create(&og).Error
}

func (o *Order) DeleteOrder(orderSn string) error {
	return DB.Where("order_sn=?", orderSn).Delete(&Order{}).Error
}

func (u *User) FindStatus(userId int) error {
	return DB.Model(&User{}).Where("id", userId).Limit(1).Find(&u).Error
}
